home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 23 code / Internet Config / IC 1.1 / ICProgKit1.1 / Source / ICeTEe / ICeTEe.p < prev    next >
Encoding:
Text File  |  1995-05-01  |  8.9 KB  |  363 lines  |  [TEXT/PJMM]

  1. unit ICeTEe;
  2.  
  3. interface
  4.  
  5.     procedure Main;
  6.  
  7. implementation
  8.  
  9.     uses
  10.         Processes, SysEqu, Notification, Traps, 
  11.  
  12.         ShowInit75, 
  13.  
  14.         ICTypes, ICCAPI;
  15.  
  16.     const
  17.         MenuFlash = $A24;
  18.         ToolScratch = $9CE;
  19.  
  20.     const
  21.         kCreator = 'ICTE';
  22.  
  23.     const
  24.         (* EXCL *)
  25.         rExclusions = 128;
  26.  
  27.         (* ICN# *)
  28.         rICTEIcon = 128;
  29.         rFailedIcon = 129;
  30.  
  31.         (* STR# *)
  32.         rErrorStrings = 128;
  33.         strMiscErr = 1;
  34.         strNoCMErr = 2;
  35.         strNoICErr = 3;
  36.         strInsufficientICErr = 4;
  37.         strNoMemoryErr = 5;
  38.         strCantFindHelperErr = 6;
  39.         strNoHelperErr = 7;
  40.         strNoURLErr = 8;
  41.  
  42.     const
  43.         noCMErr = -6660;
  44.  
  45.     type
  46.         exArray = array[1..1000] of OSType;
  47.         exPtr = ^exArray;
  48.         exHandle = ^exPtr;
  49.  
  50.         icteGlobals = record
  51.                 signature: OSType;
  52.                 version: NumVersion;
  53.                 exclusions: exHandle;
  54.                 errors: Handle;
  55.                 old_teclick: ProcPtr;
  56.             end;
  57.         icteGlobalsPtr = ^icteGlobals;
  58.         icteGlobalsPtrPtr = ^icteGlobalsPtr;
  59.  
  60.     function GetIndStrH (h: handle; index: integer): str255;
  61.     (* Stolen directly from PNL's MyStrH unit *)
  62.         var
  63.             count, i: integer;
  64.             s: str255;
  65.             ps: longInt;
  66.     begin
  67.         count := integerPtr(h^)^;
  68.         if (1 <= index) and (index <= count) then begin
  69.             ps := SizeOf(integer);
  70.             for i := 1 to index - 1 do
  71.                 ps := ps + BAND(ptr(ord(h^) + ps)^, $FF) + 1;
  72.             BlockMove(ptr(ord(h^) + ps), @s, BAND(ptr(ord(h^) + ps)^, $FF) + 1);
  73.         end
  74.         else
  75.             s := '';
  76.         GetIndStrH := s;
  77.     end;
  78.  
  79.     function DecStr (l: longint): Str32;
  80.         var
  81.             tmp: Str255;
  82.     begin
  83.         NumToString(l, tmp);
  84.         DecStr := tmp;
  85.     end; (* DecStr *)
  86.  
  87.     function GetMyGlobals: icteGlobalsPtr;
  88.     begin
  89.         GetMyGlobals := icteGlobalsPtrPtr(@Main)^;
  90.     end; (* GetMyGlobals *)
  91.  
  92.     procedure SetMyGlobals (globals: icteGlobalsPtr);
  93.         var
  94.             tmp: icteGlobalsPtrPtr;
  95.     begin
  96.         tmp := icteGlobalsPtrPtr(@Main);
  97.         tmp^ := globals;
  98.     end; (* SetMyGlobals *)
  99.  
  100.     function CurrentProcessExcluded: boolean;
  101.         var
  102.             PSN: ProcessSerialNumber;
  103.             info: ProcessInfoRec;
  104.             exclusions: exHandle;
  105.             i: integer;
  106.     begin
  107.         PSN.highLongOfPSN := 0;
  108.         PSN.lowLongOfPSN := kCurrentProcess;
  109.         info.processName := nil;
  110.         info.processAppSpec := nil;
  111.         if GetProcessInformation(PSN, info) = noErr then begin
  112.             exclusions := GetMyGlobals^.exclusions;
  113.             CurrentProcessExcluded := false;
  114.             for i := 1 to GetHandleSize(Handle(exclusions)) div 4 do begin
  115.                 if exclusions^^[i] = info.processSignature then begin
  116.                     CurrentProcessExcluded := true;
  117.                     leave;
  118.                 end; (* if *)
  119.             end; (* for *)
  120.         end
  121.         else begin
  122.             CurrentProcessExcluded := true;
  123.         end; (* if *)
  124.     end; (* CurrentProcessExcluded *)
  125.  
  126.     function HaveComponentManager: boolean;
  127.         var
  128.             response: longint;
  129.     begin
  130.         HaveComponentManager := (Gestalt(gestaltComponentMgr, response) = noErr);
  131.     end; (* HaveComponentManager *)
  132.  
  133.     function DoCommandClick (teh: TEHandle; selStart, selEnd: longint): ICError;
  134.         var
  135.             inst: ComponentInstance;
  136.             err: ICError;
  137.             err2: ICError;
  138.             text: Handle;
  139.             s: signedByte;
  140.             rgn: RgnHandle;
  141.             i: integer;
  142.             junklong: longint;
  143.     begin
  144.         if HaveComponentManager then begin
  145.             err := ICCStart(inst, kCreator);
  146.         end
  147.         else begin
  148.             err := noCMErr;
  149.         end; (* if *)
  150.         if err = noErr then begin
  151.             err := ICCFindConfigFile(inst, 0, nil);
  152.             if err = noErr then begin
  153.                 text := Handle(TEGetText(teh));
  154.                 s := HGetState(text);
  155.                 HLock(text);
  156.                 err := ICCLaunchURL(inst, '', text^, GetHandleSize(text), selStart, selEnd);
  157.                 TESetSelect(selStart, selEnd, teh);
  158.                 if err = noErr then begin
  159.                     for i := 1 to integerPtr(MenuFlash)^ do begin
  160.                         Delay(5, junklong);
  161.                         TEDeactivate(teh);
  162.                         Delay(5, junklong);
  163.                         TEActivate(teh);
  164.                     end; (* for *)
  165.                 (* leave the URL selected *)
  166.                 end; (* if *)
  167.                 HSetState(text, s);
  168.             end; (* if *)
  169.             err2 := ICCStop(inst);
  170.             if err = noErr then begin
  171.                 err := err2;
  172.             end; (* if *)
  173.         end; (* if *)
  174.         DoCommandClick := err;
  175.     end; (* DoCommandClick *)
  176.  
  177.     procedure MyNMResponseProc (nm: NMRecPtr);
  178.         var
  179.             ozone: THz;
  180.             strh: Handle;
  181.             junk: OSErr;
  182.     begin
  183.         junk := NMRemove(nm);
  184.         ozone := GetZone;
  185.         SetZone(SystemZone);
  186.         strh := RecoverHandle(Ptr(nm^.nmStr));
  187.         if strh <> nil then begin
  188.             DisposeHandle(strh);
  189.         end; (* if *)
  190.         DisposePtr(Ptr(nm));
  191.         SetZone(ozone);
  192.     end; (* MyNMResponseProc *)
  193.  
  194.     procedure MyTEClick (teh: TEHandle; old_selStart, old_selEnd: integer);
  195.         var
  196.             err: ICError;
  197.             message: Str255;
  198.             nm: NMRecPtr;
  199.             strindex: integer;
  200.             strh: StringHandle;
  201.     begin
  202.         if not CurrentProcessExcluded then begin
  203.             if not ((old_selStart <= teh^^.selStart) and (teh^^.selStart <= old_selEnd) and (old_selStart <= teh^^.selEnd) and (teh^^.selEnd <= old_selEnd)) then begin
  204.                 old_selStart := teh^^.selStart;
  205.                 old_selEnd := teh^^.selEnd;
  206.             end; (* if *)
  207.             err := DoCommandClick(teh, old_selStart, old_selEnd);
  208.             if err <> noErr then begin
  209.                 (* can't case on the error codes because MPW Pascal does not case on longints properly *)
  210.                 if err = badComponentInstance then begin
  211.                     strindex := strNoICErr;
  212.                 end
  213.                 else if err = noCMErr then begin
  214.                     strindex := strNoCMErr;
  215.                 end
  216.                 else if err = badComponentSelector then begin
  217.                     strindex := strInsufficientICErr;
  218.                 end
  219.                 else if err = memFullErr then begin
  220.                     strindex := strNoMemoryErr;
  221.                 end
  222.                 else if err = afpItemNotFound then begin
  223.                     strindex := strCantFindHelperErr;
  224.                 end
  225.                 else if err = icPrefNotFoundErr then begin
  226.                     strindex := strNoHelperErr;
  227.                 end
  228.                 else if err = icNoURLErr then begin
  229.                     strindex := strNoURLErr;
  230.                 end
  231.                 else begin
  232.                     strindex := strMiscErr;
  233.                 end; (* if *)
  234.                 message := GetIndStrH(GetMyGlobals^.errors, strindex);
  235.                 strindex := Pos('^0', message);
  236.                 if strindex <> 0 then begin
  237.                     Delete(message, strindex, 2);
  238.                     Insert(DecStr(err), message, strindex);
  239.                 end; (* if *)
  240.                 strh := NewString(message);
  241.                 HLock(Handle(strh));
  242.                 nm := NMRecPtr(NewPtrSysClear(sizeof(NMRec)));
  243.                 if nm <> nil then begin
  244.                     nm^.qType := ord(nmType);
  245.                     nm^.nmMark := 0;
  246.                     nm^.nmIcon := nil;
  247.                     nm^.nmSound := nil;
  248.                     nm^.nmStr := strh^;
  249.                     nm^.nmResp := @MyNMResponseProc;
  250.                     err := NMInstall(nm);
  251.                 end
  252.                 else begin
  253.                     SysBeep(10);
  254.                 end; (* if *)
  255.             end; (* if *)
  256.         end; (* if *)
  257.     end; (* MyTEClick *)
  258.  
  259.     procedure CallTEClick (pt: Point; fExtend: boolean; teh: TEHandle; proc: ProcPtr);
  260.     inline
  261.         $205F, (* move.l    (a7)+,a0            ; pop proc address    *)
  262.         $4E90; (* jsr            (a0)                ; call proc                *)
  263.  
  264.     procedure PascalTEClickPatch (pt: Point; fExtend: boolean; teh: TEHandle);
  265.         var
  266.             old_selStart, old_selEnd: integer;
  267.             globals: icteGlobalsPtr;
  268.             ozone: THz;
  269.             command_key: boolean;
  270.             km: KeyMap;
  271.     begin
  272.         globals := GetMyGlobals;
  273.         old_selStart := teh^^.selStart;
  274.         old_selEnd := teh^^.selEnd;
  275.         GetKeys(km);
  276.         command_key := km[55];
  277.         CallTEClick(pt, fExtend, teh, globals^.old_teclick);
  278.         if command_key then begin
  279.             ozone := GetZone;
  280.             SetZone(SystemZone);
  281.             MyTEClick(teh, old_selStart, old_selEnd);
  282.             SetZone(ozone);
  283.         end; (* if *)
  284.     end; (* PascalTEClickPatch *)
  285.  
  286.     function MyGestalt (selector: OSType; var response: longint): OSErr;
  287.         var
  288.             globals: icteGlobalsPtr;
  289.     begin
  290.         globals := GetMyGlobals;
  291.         response := longint(globals);
  292.         MyGestalt := noErr;
  293.     end; (* MyGestalt *)
  294.  
  295.     procedure Main;
  296.         var
  297.             ozone: THz;
  298.             err: OSErr;
  299.             err2: OSErr;
  300.             response: longint;
  301.             globals: icteGlobalsPtr;
  302.             exclusions: Handle;
  303.             errors: Handle;
  304.             vers: VersRecHndl;
  305.     begin
  306. (* Debugger; *)
  307.         (* detach our resource *)
  308.         DetachResource(RecoverHandle(Ptr(longintPtr(ToolScratch)^)));
  309.         ShowIcon7(rICTEIcon, false);
  310.         ozone := GetZone;
  311.         SetZone(SystemZone);
  312.         (* check for System 7 *)
  313.         err := noErr;
  314.         if (Gestalt(gestaltSystemVersion, response) <> noErr) | (response < $700) then begin
  315.             err := unimpErr;
  316.         end; (* if *)
  317.         (* create the globals *)
  318.         if err = noErr then begin
  319.             globals := icteGlobalsPtr(NewPtrSysClear(sizeof(icteGlobals)));
  320.             err := MemError;
  321.         end; (* if *)
  322.         if err = noErr then begin
  323.             (* install globals *)
  324.             SetMyGlobals(globals);
  325.             globals := GetMyGlobals;
  326.             (* init globals *)
  327.             globals^.signature := kCreator;
  328.             vers := VersRecHndl(Get1Resource('vers', 1));
  329.             if vers <> nil then begin
  330.                 globals^.version := vers^^.numericVersion;
  331.             end; (* if *)
  332.             exclusions := Get1Resource('EXCL', rExclusions);
  333.             err := HandToHand(exclusions);
  334.             globals^.exclusions := exHandle(exclusions);
  335.             errors := Get1Resource('STR#', rErrorStrings);
  336.             err2 := HandToHand(errors);
  337.             globals^.errors := errors;
  338.             if err = noErr then begin
  339.                 err := err2;
  340.             end; (* if *)
  341.         end; (* if *)
  342.         (* register gestalt *)
  343.         if err = noErr then begin
  344.             err := NewGestalt(kCreator, @MyGestalt);
  345.         end; (* if *)
  346.         if err = noErr then begin
  347.         (* install our patch *)
  348.             globals^.old_teclick := ProcPtr(NGetTrapAddress(_TEClick, ToolTrap));
  349.             NSetTrapAddress(longint(@PascalTEClickPatch), _TEClick, ToolTrap);
  350.         end; (* if *)
  351.         (* if we got an error then we bleed memory all over the place, this is not an accident *)
  352.         (* how many copies of the init can you reasonably fail to install??? *)
  353.         SetZone(ozone);
  354.  
  355.         if err = noErr then begin
  356.             ShowIcon7(rICTEIcon, true);
  357.         end
  358.         else begin
  359.             ShowIcon7(rFailedIcon, true);
  360.         end; (* if *)
  361.     end; (* Main *)
  362.  
  363. end. (* ICeTEe *)